<template>
  <div>
    <el-dialog
      :title="info.isadd == true ? '会员添加' : '会员编辑'"
      :visible.sync="info.isshow"
      @closed="close"
    >
      <el-form :model="form">
        <el-form-item label="手机号" label-width="120px">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="会员名称" label-width="120px">
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户密码" label-width="120px">
          <el-input
            v-model="form.password"
            autocomplete="off"
            :placeholder="info.isadd ? '' : '密码空置则不修改'"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="info.isshow = false">取 消</el-button>
        <el-button type="primary" @click="add" v-if="info.isadd"
          >提交</el-button
        >
        <el-button type="primary" v-else @click="edit">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { reqmemberedit, reqmemberinfo, requseradd } from "../../../request/api";
import { successAlert, errorAlert } from "../../../utils/alert";
import { mapActions, mapGetters } from "vuex";
export default {
  props: ["info"],
  data() {
    return {
      form: {
        uid: "",
        phone: "",
        nickname: "",
        password: "",
        status: 1,
      },
    };
  },
  methods: {
    ...mapActions({
      reqList: "member/reqList",
    }),
    empty() {
      this.form = {
        uid: "",
        phone: "",
        nickname: "",
        password: "",
        status: 1,
      };
    },
    add() {
      // console.log(this.form);
      // console.log(this.$refs.tree.getCheckedKeys());
      console.log(this.form);
      requseradd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.empty();
          this.info.isshow = false;
          this.$emit("init");
        } else {
          errorAlert(res.data.msg);
        }
      });
    },
    getOne(id) {
      reqmemberinfo({ uid: id }).then((res) => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          this.form.password = "";
          // this.$refs.tree.setCheckedKeys(JSON.parse(this.form.menus));
        }
      });
    },
    edit() {
      // this.form.menus = JSON.stringify(this.$refs.tree.getCheckedKeys());
      reqmemberedit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.info.isshow = false;
          this.empty();
          this.reqList();
        }
      });
    },
    close() {
      !this.info.isadd && this.empty();
    },
  },
  mounted() {},
  computed: {
    ...mapGetters({
      list: "member/list",
    }),
  },
};
</script>

<style></style>
